# -*- coding: utf-8 -*-
import json
from Core.CC import get_app_path
from Conf import JSON_DIR

_py = "Comps/Json.py"

'''
Json管理模块
'''

class Json:
    def __init__(self):
        self.py = _py
        self.json_dir = get_app_path() + JSON_DIR
        self.da = {}
        self.if_open = False

    def open(self, json_dir=None):
        try:
            if json_dir:
                self.json_dir = get_app_path() + json_dir

            with open(self.json_dir, 'r', encoding='utf-8') as f:
                content = f.read()
                if content.startswith(u'\ufeff'):
                    content = content.encode('utf8')[3:].decode('utf8')
                self.da = json.loads(content)
            f.close()
            self.if_open = True
        except Exception as err:
            print(err)

    def save(self, json_dir=None):
        if not self.if_open:
            return

        if json_dir:
            self.json_dir = get_app_path() + json_dir

        with open(self.json_dir, "w", encoding='utf-8') as f:
            json.dump(self.da, f, indent=4, ensure_ascii=False)
        f.close()